package com.ruoyi.paint.admin.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.paint.admin.domain.ImExportDto.TqColorschemesImport;
import com.ruoyi.paint.admin.domain.ImExportDto.TqSchemetintsAllImport;
import com.ruoyi.paint.admin.domain.TqCars;
import com.ruoyi.paint.admin.domain.TqColorschemes;
import com.ruoyi.paint.admin.mapper.TqColorschemesMapper;
import com.ruoyi.paint.admin.service.ITqCarsService;
import com.ruoyi.paint.admin.service.TqColorschemesService;
import com.ruoyi.paint.admin.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Asus
 * @description 针对表【tq_colorschemes(配色方案表)】的数据库操作Service实现
 * @createDate 2024-07-03 11:58:41
 */
@Service
public class TqColorschemesServiceImpl extends ServiceImpl<TqColorschemesMapper, TqColorschemes>
        implements TqColorschemesService {

    @Autowired
    private ITqCarsService tqCarsService;
    @Autowired
    private TqColorschemesService tqColorschemesService;

    @Override
    @Transactional
    public List<String> saveBatchService(List<TqColorschemesImport> importDatas) {
        List<String> failureMessages = new ArrayList<>();
        for (TqColorschemesImport importData : importDatas) {
            try {
                LambdaQueryWrapper<TqCars> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(TqCars::getBrand, importData.getBrand())
                        .eq(TqCars::getModel, importData.getModel())
                        .eq(TqCars::getColorName, importData.getColorName());
                List<TqCars> carsList = tqCarsService.list(wrapper);

                if (carsList.isEmpty()) {
                    String message = "未找到匹配的汽车信息: " + JSON.toJSONString(importData);
                    failureMessages.add(message);
                    throw new ServiceException(message);
                } else if (carsList.size() > 1) {
                    Set<Long> carIds = carsList.stream().map(TqCars::getId).collect(Collectors.toSet());
                    if (carIds.size() != 1) {
                        String message = "查询到的汽车信息和其他的不一致: " + JSON.toJSONString(importData);
                        failureMessages.add(message);
                        throw new ServiceException(message);
                    }
                }
                Long carId = carsList.get(0).getId();
                TqColorschemes tqColorschemes = BeanCopyUtils.copyBean(importData, TqColorschemes.class);
                tqColorschemes.setCarId(carId);
                if (importData.getOriginalColorCode() != null) {
                    tqColorschemes.setOriginalColorCode(importData.getOriginalColorCode());
                } else {
                    tqColorschemes.setOriginalColorCode("");
                }
                tqColorschemesService.save(tqColorschemes);

            } catch (Exception e) {
                log.error("处理颜色方案数据时发生错误", e);
                failureMessages.add("错误：" + importData.toString() + ", 原因：" + e.getMessage());
            }
        }
        failureMessages.add("配色方案导入成功：" + importDatas.size() + "条数据");
        return failureMessages;
    }

    @Override
    public int saveBatchServiceAll(List<TqSchemetintsAllImport> importDatas) {
        List<String> failureMessages = new ArrayList<>();
        int successNum = 0;
        int failureNum = 0;
        List<String> failureMsg = new ArrayList<>();
        // 遍历所有要导入的数据

        for (TqSchemetintsAllImport importData : importDatas) {

            // 检查品牌、型号和颜色名称是否为空
            if (StringUtils.isBlank(importData.getBrand()) ||
                    StringUtils.isBlank(importData.getModel()) ||
                    StringUtils.isBlank(importData.getColorName()) ||
                    importData.getYear() == null ||
                    StringUtils.isBlank(importData.getPaintBrand())) {
                failureNum++;
                failureMsg.add("、车辆导入失败：");
                if (StringUtils.isBlank(importData.getBrand())) failureMsg.add("品牌为空");
                if (StringUtils.isBlank(importData.getModel())) failureMsg.add("型号为空");
                if (StringUtils.isBlank(importData.getColorName())) failureMsg.add("颜色名称为空");
                if (importData.getYear() == null) failureMsg.add("年款为空");
                if (StringUtils.isBlank(importData.getPaintBrand())) failureMsg.add("油漆名称为空");
                log.error(failureMsg.toString());
                continue;
            }
            // 查询汽车信息
            LambdaQueryWrapper<TqCars> carWrapper = new LambdaQueryWrapper<>();
            carWrapper.eq(TqCars::getBrand, importData.getBrand())
                    .eq(TqCars::getModel, importData.getModel())
                    .eq(TqCars::getColorName, importData.getColorName())
                    .eq(TqCars::getYear, importData.getYear())
                    .eq(TqCars::getPaintBrand, importData.getPaintBrand());
            List<TqCars> carsList = tqCarsService.list(carWrapper);

            if (carsList.isEmpty()) {
                String message = "未找到匹配的汽车信息: " + JSON.toJSONString(importData);
                failureMessages.add(message);
                throw new ServiceException(message);
            } else if (carsList.size() > 1) {
                Set<Long> carIds = carsList.stream().map(TqCars::getId).collect(Collectors.toSet());
                if (carIds.size() != 1) {
                    String message = "查询到的汽车信息ID不一致: " + JSON.toJSONString(importData);
                    failureMessages.add(message);
                    throw new ServiceException(message);
                }
            }
            Long carId = carsList.get(0).getId();

            // 创建配色方案实体
            TqColorschemes tqColorschemes = BeanCopyUtils.copyBean(importData, TqColorschemes.class);
            tqColorschemes.setCarId(carId);
            if (importData.getOriginalColorCode() != null) {
                tqColorschemes.setOriginalColorCode(importData.getOriginalColorCode());
            } else {
                tqColorschemes.setOriginalColorCode("");
            }

            // 检查是否有相同的配色方案已存在
            LambdaQueryWrapper<TqColorschemes> schemeWrapper = new LambdaQueryWrapper<>();
            schemeWrapper.eq(TqColorschemes::getCarId, carId)
                    .eq(TqColorschemes::getTitle, importData.getTitle());
            List<TqColorschemes> list = tqColorschemesService.list(schemeWrapper);

            if (list.isEmpty()) {
                // 只有当不存在相同的配色方案时才保存
                tqColorschemesService.save(tqColorschemes);
                successNum++;
            }
        }

        return successNum;
    }

}




